<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
      <el-form label-position="right" :model="employeeForm" :label-width="formLabelWidth" disabled="disabled">
        <el-row>
          <el-col :span="11">
            <el-form-item label="企业名称">
              <el-input v-model="employeeForm.enterpriseName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="工号">
              <el-input v-model="employeeForm.employeeNumber"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="姓名">
              <el-input v-model="employeeForm.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="密码">
              <el-input v-model="employeeForm.password"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="性别">
              <el-select v-model="employeeForm.gender" placeholder="请选择性别">
                <el-option label="男" :value="1"></el-option>
                <el-option label="女" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="出生日期">
              <el-input v-model="employeeForm.birthDate"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="联系电话">
              <el-input v-model="employeeForm.contactPhone"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="邮箱">
              <el-input v-model="employeeForm.email"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="省/市/区">
              <el-cascader
                v-model="provinceCityArea"
                :options="provinceCityAreaOptions"
                :props="{ expandTrigger: 'hover', value: 'code', label: 'name' }"/>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="家庭地址">
              <el-input v-model="employeeForm.address"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="聘用日期">
              <el-input v-model="employeeForm.hireDate"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="部门名称">
              <el-input v-model="employeeForm.departmentName"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="岗位名称">
              <el-input v-model="employeeForm.positionName"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="创建时间">
              <el-input v-model="employeeForm.createTime"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="最后修改时间">
              <el-input v-model="employeeForm.modifyTime"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="12" :offset="6">
          <div class="form-button-container">
            <el-button @click="onBack" type="primary">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineEmits, defineProps, inject, onMounted, reactive, ref, toRefs } from 'vue'
import employeeApi from '@/api/employeeApi'
import type { Employee } from '@/types/resp/employee'
import type { Id } from '@/types/id'
import areas from '@/locales/area.json'

const employeeForm = reactive<Partial<Employee>>({});
const provinceCityArea = ref<string[]>([])
const provinceCityAreaOptions = areas.provinces
const props = defineProps<Id>()
const emit = defineEmits<{
  (e: 'closeEmployeeViewEvent'): void;
}>()
const formLabelWidth = ref<string>('')

onMounted(() => {
  initEmployeeById(props.id)
  const globalState = inject('globalState', {} as { formLabelWidth?: string })
  if (!globalState) {
    throw new Error('globalState is not provided');
  }
  const globalStateRefs = toRefs(globalState)
  if (globalStateRefs.formLabelWidth) {
    formLabelWidth.value = globalStateRefs.formLabelWidth.value || ''
  }
})

const initEmployeeById = async (id: number) => {
  const resp = await employeeApi.findById(id)
  if (resp && resp.code === 1 && resp.data) {
    Object.assign(employeeForm, resp.data)
    provinceCityArea.value = []
    if (employeeForm.province) {
      provinceCityArea.value.push(employeeForm.province)
    }
    if (employeeForm.city) {
      provinceCityArea.value.push(employeeForm.city)
    }
    if (employeeForm.area) {
      provinceCityArea.value.push(employeeForm.area)
    }
  }
}
const onBack = () => {
  emit('closeEmployeeViewEvent')
}
</script>

<style lang="scss">
</style>
